package com.cnzhy.mybatis.plus.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.function.Predicate;

/**
 * @author 赵 岩
 * Swagger管理页面 http://localhost:8080/swagger-ui.html
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {

    /**
     * 该套 API 说明，包含作者、简介、版本、host、服务URL
     */
    private ApiInfo apiInfo() {
        Contact contact = new Contact("zhaoyan", "www.zhaoyan5.com", "cnzhaoyan@foxmail.com");
        return new ApiInfoBuilder().title("demo api 说明").contact(contact).version("0.1").build();
    }

    @Bean
    public Docket createRestApi() {
        Predicate<RequestHandler> basePackage = RequestHandlerSelectors.basePackage("com.cnzhy")::apply;
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(basePackage::test).paths(PathSelectors.any()).build();
    }

}
